<template>
  <div class="box">
    <header class="header">
      <van-nav-bar
        :title="totalNum > 0 ? '购物车' + totalNum : '购物车'"
        left-arrow
        @click-left="$router.back()"
      />
    </header>
    <div class="content">
      <div class="noShop" v-if="flag">
        <van-empty description="购物车空空如也">
          <van-button @click="$router.push('/kind')" round type="danger" class="bottom-button">立即购物</van-button>
        </van-empty>
      </div>
      <div v-else class="hasShop">
        <van-swipe-cell
          v-for="item of cartList"
          :key="item.cartid">
          <van-row class="row">
            <van-col span="2" class="col">
              <van-checkbox @change="selectOneFn(item.cartid, item.flag)" v-model="item.flag" class="mycheckbox"></van-checkbox>
            </van-col>
            <van-col span="22">
              <van-card
                :num="item.num"
                :price="item.originprice"
                :title="item.proname"
                :thumb="item.img1"
              >
                <template #num>
                  <van-stepper @change="updateNumFn(item.cartid, item.num)" v-model="item.num" theme="round"/>
                </template>
              </van-card>
            </van-col>
          </van-row>
          <template #right>
            <van-button @click="deleteItem(item.cartid)" square type="danger" text="删除" class="delete-button"/>
          </template>
        </van-swipe-cell>
        <van-submit-bar :price="totalPrice" button-text="提交订单" @submit="onSubmit">
          <van-checkbox v-model="checked" @click="selectAllFn">全选</van-checkbox>
        </van-submit-bar>
      </div>
      <van-divider>猜你喜欢</van-divider>
      <pro-list :list="recommendList"></pro-list>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { Divider, Col, Row, NavBar, Empty, Button, Card, Stepper, SubmitBar, Checkbox, SwipeCell, Dialog } from 'vant'
import { getCartList, remove, updateNum, selectall, selectone, getRecommendList } from './../../api/cart'
import ProList from './../../components/ProList'
Vue.use(Divider)
Vue.use(NavBar)
Vue.use(Empty)
Vue.use(Button)
Vue.use(Card)
Vue.use(Stepper)
Vue.use(SubmitBar)
Vue.use(Checkbox)
Vue.use(SwipeCell)
Vue.use(Dialog)
Vue.use(Col)
Vue.use(Row)
export default {
  components: {
    ProList
  },
  data () {
    return {
      cartList: [],
      recommendList: [],
      flag: true,
      checked: false
    }
  },
  methods: {
    onSubmit () {
    },
    getCartListFn () {
      getCartList({ userid: localStorage.getItem('userid') }).then(res => {
        console.log(res.data)
        if (res.data.code === '10020') {
          this.flag = true
          this.cartList = [] // 依赖性 - 总价和总数
        } else {
          this.flag = false
          this.cartList = res.data.data
          this.checked = this.cartList.every(item => item.flag)
        }
      })
    },
    deleteItem (cartid) {
      Dialog.confirm({
        message: '便宜不等人，请三思而行',
        confirmButtonText: '残忍离去'
      }).then(() => {
        remove({ cartid }).then(res => {
          this.getCartListFn()
        })
      }).catch(() => {})
    },
    updateNumFn (cartid, num) {
      // console.log(cartid, num)
      updateNum({ cartid, num }).then(() => {
        this.getCartListFn()
      })
    },
    selectAllFn () {
      selectall({
        userid: localStorage.getItem('userid'),
        type: this.checked
      }).then(() => {
        this.getCartListFn()
      })
    },
    selectOneFn (cartid, flag) {
      selectone({ cartid, flag }).then(() => {
        this.getCartListFn()
      })
    }
  },
  mounted () {
    if (localStorage.getItem('isLogin') === 'true') {
      this.getCartListFn()
    } else {
      this.$router.push('/login')
    }

    getRecommendList().then(res => {
      this.recommendList = res.data.data
    })
  },
  computed: {
    totalNum () {
      return this.cartList.reduce((sum, item) => {
        return item.flag ? item.num + sum : sum + 0
      }, 0)
    },
    totalPrice () {
      return this.cartList.reduce((sum, item) => {
        return item.flag ? item.num * item.originprice + sum : sum + 0
      }, 0) * 100
    }
  }
}
</script>

<style lang="stylus" scoped>
.delete-button {
  height: 100%;
}
.mycheckbox
  margin-top 42px
  margin-left 10px
.van-card
  background-color #ffffff
</style>
